package by.dkusch.aircompany.webapp.page.staff.panel;

import java.util.ArrayList;
import java.util.Arrays;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.ListModel;

import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Person;
import by.dkusch.aircompany.model.tables.Person_;
import by.dkusch.aircompany.model.tables.enums.Gender;
import by.dkusch.aircompany.model.tables.enums.PersonPosition;
import by.dkusch.aircompany.services.PersonService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.validator.UniqueFieldValidator;

public class PersonEditPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@Inject
	private PersonService personService;

	public PersonEditPanel(String id, final Person person, final PageCreator pageCreator, ActionType action) {
		super(id);

		Form<Person> form = new Form<Person>("form", new CompoundPropertyModel<Person>(person));

		final TextField<String> tfNameFirst = new TextField<String>("firstName");
		tfNameFirst.add(new PropertyValidator<String>()).setLabel(new ResourceModel("person.list.name.first"));
		form.add(tfNameFirst);

		final TextField<String> tfNameSecond = new TextField<String>("secondName");
		tfNameSecond.add(new PropertyValidator<String>()).setLabel(new ResourceModel("person.list.name.second"));

		form.add(tfNameSecond);

		final TextField<String> tfNameLast = new TextField<String>("lastName");
		tfNameLast.add(new PropertyValidator<String>()).setLabel(new ResourceModel("person.list.name.last"));

		form.add(tfNameLast);

		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>());
		form.add(chBArc);

		form.add(new Label("correctDt"));

		final DropDownChoice<Gender> chGender = new DropDownChoice<Gender>("gender", Arrays.asList(Gender.values()), new EnumChoiceRenderer<Gender>(this));
		chGender.add(new PropertyValidator<Gender>()).setLabel(new ResourceModel("person.list.gender"));

		form.add(chGender);

		final DropDownChoice<PersonPosition> chPos = new DropDownChoice<PersonPosition>("posId", Arrays.asList(PersonPosition.values()), new EnumChoiceRenderer<PersonPosition>(this));
		chPos.add(new PropertyValidator<PersonPosition>()).setLabel(new ResourceModel("person.list.position"));

		form.add(chPos);

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				super.onSubmit();
				personService.saveOrUpdate(person);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

		};

		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		final TextField<String> tfTabCode = new TextField<String>("tabCode");
		tfTabCode.add(new PropertyValidator<String>()).setLabel(new ResourceModel("person.list.tabcode"));
		tfTabCode.add(new UniqueFieldValidator<Long, Person>(Person_.tabCode, personService, person)).setLabel(new ResourceModel("person.list.tabcode"));
		form.add(tfTabCode);

		person.setCrewSet(personService.getCrewSet(person.getId()));
		IChoiceRenderer<Crew> renderer = new ChoiceRenderer<Crew>("code");

		ListChoice<Crew> listChoice = new ListChoice<Crew>("crewSet", new ListModel<Crew>(new ArrayList<Crew>(person.getCrewSet())), renderer);

		form.add(listChoice.setEnabled(false));

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});

		add(form);
		form.setEnabled(action.equals(ActionType.EDIT));

	}

}
